var $jq = jQuery.noConflict();
var limit = 300;
(function($jq) {
    $jq.fn.extend({
        limiter: function(limit, elem) {
            $jq(this).on("keyup focus", function() {
                setCount(this, elem);
            });
            function setCount(src, elem) {
                var chars = src.value.length;
                if (chars > limit) {
                    src.value = src.value.substr(0, limit);
                    chars = limit;
                }
                elem.html(limit - chars);
            }
            setCount($jq(this)[0], elem);
        }
    });
})(jQuery);
jQuery(document).ready(function() {

    jQuery("#edit-disability-explanation").live("focus", function() {
        $jq("#disable-limit").parent('div').css('display', 'block');
        var elem = $jq("#disable-limit");
        $jq(this).limiter(limit, elem);
    });
    jQuery("#edit-allergy-explanation").live("focus", function() {
        $jq("#allergy-limit").parent('div').css('display', 'block');
        var elem = $jq("#allergy-limit");
        $jq(this).limiter(limit, elem);
    });
    jQuery("#edit-medication-explanation").live("focus", function() {
        $jq("#medication-limit").parent('div').css('display', 'block');
        var elem = $jq("#medication-limit");
        $jq(this).limiter(limit, elem);
    });
});


/* end of text area limiter*/

jQuery(document).ready(function() {
    jQuery("#edit-is-union-member-no").live("change", function() {
        if (jQuery(this).is(':checked')) {
            jQuery(".form-item-is-min-income").show();
        } else {
            jQuery(".form-item-is-min-income").hide();
        }
    });
    jQuery("#edit-is-union-member-yes").live("change", function() {
        if (jQuery(this).is(':checked')) {
            jQuery(".form-item-is-min-income").hide();
        }
    });

    jQuery(".form-item-disabilities input:radio").live("change", function() {
        if (jQuery(this).is(':checked') && jQuery(this).val().toLowerCase() == "1") {
            jQuery(".form-item-disability-explanation").show();
            $jq("#disable-limit").parent('div').css('display', 'block');
        } else {
            jQuery(".form-item-disability-explanation").hide();
            jQuery("#edit-disability-explanation").val('');
            $jq("#disable-limit").text(limit).parent('div').css('display', 'none');
        }
    });

    jQuery(".form-item-medication-allergy input:radio").live("change", function() {
        if (jQuery(this).is(':checked') && jQuery(this).val().toLowerCase() == "1") {
            jQuery(".form-item-allergy-explanation").show();
            $jq("#allergy-limit").parent('div').css('display', 'block');
        } else {
            jQuery(".form-item-allergy-explanation").hide();
            jQuery("#edit-allergy-explanation").val('');
            $jq("#allergy-limit").text(limit).parent('div').css('display', 'none');
        }
    });

    jQuery(".form-item-taking-medication input:radio").live("change", function() {
        if (jQuery(this).is(':checked') && jQuery(this).val().toLowerCase() == "1") {
            jQuery(".form-item-medication-explanation").show();
            $jq("#medication-limit").parent('div').css('display', 'block');
        } else {
            jQuery(".form-item-medication-explanation").hide();
            jQuery("#edit-medication-explanation").val('');
            $jq("#medication-limit").text(limit).parent('div').css('display', 'none');
        }
    });

    jQuery("#edit-yp-alternate-contact-texting").live("change", function() {
        if (jQuery(this).is(':checked')) {
            jQuery(".form-item-yp-text-carrier").show();
            $jq(".form-item-texting-phone").show();
        } else {
            jQuery(".form-item-yp-text-carrier").hide();
            $jq(".form-item-texting-phone").hide();
        }
    });

    jQuery("#edit-alternate-contact-texting").live("change", function() {
        if (jQuery(this).is(':checked')) {
            jQuery(".form-item-text-carrier").show();
        } else {
            jQuery(".form-item-text-carrier").hide();
        }
    });
    jQuery("#edit-alternate-contact-texting").trigger("change");
    jQuery(".form-item-is-union-member input:radio").trigger("change");
    jQuery(".form-item-disabilities input:radio").trigger("change");
    jQuery(".form-item-medication-allergy input:radio").trigger("change");
    jQuery(".form-item-taking-medication input:radio").trigger("change");
});


jQuery('#edit-cell-phone, #edit-phone-number , #edit-agent-phone , #edit-emergency-contact-emergency-phone , #edit-em-parents-0-phone-1, #edit-em-parents-1-phone-1 , #edit-em-parents-0-phone-2 , #edit-em-parents-1-phone-2,#edit-yp-cell-phone,#edit-yp-phone-number').live("focus", function(event) {

    jQuery(this).mask("999-999-9999");
});
jQuery('#edit-parent-addresses-0-zip, #edit-parent-addresses-1-zip, #edit-parent-schools-0-school-zip,#edit-parent-schools-1-school-zip,#edit-parent-schools-2-school-zip,#edit-parent-schools-3-school-zip,#edit-emergency-contact-emergency-zip').live("focus", function(event) {

    jQuery(this).mask("99999");
});
jQuery('#edit-em-addresses-0-zip, #edit-em-addresses-1-zip, #edit-em-schools-0-school-zip,#edit-em-schools-1-school-zip,#edit-em-schools-2-school-zip,#edit-em-schools-3-school-zip,#edit-emergency-contact-emergency-zip').live("focus", function(event) {

    jQuery(this).mask("99999");
});
jQuery("#registration-em-form").live("focus", function(event) {
    jQuery(this).validate({
        errorElement: "p",
        errorClass: "form-error",
        rules: {
            'edit-email': {
                required: true,
                email: true
            }
        },
        messages: {
            'edit-email': "Please enter a valid email address"
        }
    });
});
jQuery("##edit-save-draft").live("click", function(event) {

    var formId = jQuery(this).closest('form').attr('id');
    jQuery('#edit-is-draft').val('1');
    JQuery("#" + formId).get(0).setAttribute('action', 'index.php');

    //return false;
});
//window.alert = function(arg) { if (window.console && console.log) { console.log(arg);}};
//(function ($jq) {
//   $jq(document).ready(function(){
//   }); // END document ready
//})(jQuery);